﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Windows.Controls;
using Telerik.Windows.Documents.Fixed;
using Telerik.Windows.SyntaxEditor.Core.Tagging;
using Telerik.Windows.SyntaxEditor.Core.Text;

namespace Chaotic.Module.DBTool.Common
{
    public class MultiLineCommentTagger : ITagger<ClassificationTag>
    {
        private readonly RadSyntaxEditor editor;
        private readonly ClassificationType commentClassificationType;

        public MultiLineCommentTagger(RadSyntaxEditor syntaxEditor)
        {
            editor = syntaxEditor;

            // 创建注释的分类类型（用于样式定义）
            commentClassificationType = new ClassificationType("Comment");

            // 监听文档变化
            editor.Document.TextContentChanging += Document_TextContentChanging;
            editor.Document.TextContentChanged += Document_TextContentChanged;
        }

        private void Document_TextContentChanged(object? sender, TextContentChangedEventArgs e)
        { 
        }

        private void Document_TextContentChanging(object? sender, TextContentChangingEventArgs e)
        {
            // 文档变化前的处理
        }

        public IEnumerable<TagSpan<ClassificationTag>> GetTags(NormalizedSnapshotSpanCollection spans)
        {
            if (spans.Count == 0)
                yield break;

            var snapshot = spans[0].Snapshot;
            var text = snapshot.GetText();

            int currentIndex = 0;

            while (currentIndex < text.Length)
            {
                // 查找注释开始 "/*"
                int commentStart = text.IndexOf("/*", currentIndex, StringComparison.Ordinal);
                if (commentStart == -1) break;

                // 查找注释结束 "*/"
                int commentEnd = text.IndexOf("*/", commentStart + 2, StringComparison.Ordinal);
                if (commentEnd == -1)
                {
                    // 如果没有找到结束标记，注释一直到文档末尾
                    commentEnd = text.Length;
                }
                else
                {
                    commentEnd += 2; // 包含 "*/"
                }

             
            }
        }

 
        public event EventHandler<TagsChangedEventArgs> TagsChanged;
    }
}
